public class ConnectionController 
{

     Case connCase;
     Connection_Log__c connectionLog;
     Account vpnAccount;
     Account serverAccount;
     List<connection_log__c> loglist = New List<connection_log__c>();
      
     public Case getCase() 
     {
        if(ApexPages.currentPage().getParameters().get('id') != null)
        {
	        connCase = [select Id
	        ,account.Force_VPN_Updates_From_Parent__c
	        ,account.Parent.Id
	        ,account.Parent.Force_VPN_Updates_From_Parent__c
	        ,account.Parent.Parent.Id
	        ,account.Parent.Parent.Force_VPN_Updates_From_Parent__c
	        ,ContactId
	        ,AccountId
	        ,Account.Name
	        ,contact.Email
	        ,account.System_Access_Email__c
	        ,account.Server_Connection_Info__c
	        ,account.VPN_Connection_Info__c 
	        from Case where id = :ApexPages.currentPage().getParameters().get('id')];
        }
        return connCase;      
     }
     
     public Account getServerAccount()
     {
     	Try
     	{
	     	serverAccount = [select id
	     	,name
	     	,System_Access_Email__c
	     	,server_connection_Info__c
	     	,VPN_Review_Needed__c 
	     	,Force_VPN_Updates_From_Parent__c 
	     	,Parent.Id
	        ,Parent.Force_VPN_Updates_From_Parent__c
	        ,Parent.Parent.Id
	        ,Parent.Parent.Force_VPN_Updates_From_Parent__c
	     	from Account where id = :ApexPages.currentPage().getParameters().get('accountId')];
     	}
     	catch(ListException e)
     	{
     		system.debug('The Connection Log Utility was launched from an unauthorized location, or the accountId was removed from the URL');
     	}
     	return serverAccount;
     }
     
     public Account getVpnAccount()
     {
		getServerAccount();
     	
     	if(((serverAccount.Force_VPN_Updates_From_Parent__c == true)&(serverAccount.Parent.Force_VPN_Updates_From_Parent__c != true))&(serverAccount.Parent.Id != Null))
     	{
     		vpnAccount = [select VPN_Connection_Info__c,VPN_Review_Needed__c from Account where id = :serverAccount.Parent.Id];
     	}
     	else if(((serverAccount.Force_VPN_Updates_From_Parent__c == true)&(serverAccount.Parent.Force_VPN_Updates_From_Parent__c == true))&(serverAccount.Parent.Parent.Id != Null))
     	{
     		vpnAccount = [select VPN_Connection_Info__c,VPN_Review_Needed__c from Account where id = :serverAccount.Parent.Parent.Id];     		
     	}
     	else
     	{
     		vpnAccount = [select VPN_Connection_Info__c,VPN_Review_Needed__c from Account where id = :serverAccount.Id];
     	}
     	return vpnAccount;
     }
      
     public Connection_Log__c getConnectionLog()
     {
        if(connectionLog == null) connectionLog = new Connection_Log__c();
        return connectionLog;
     }
     
     public List<connection_log__c> getLogList()
     {
     	logList = [select case__r.id,case__r.casenumber,owner.alias,createddate from connection_log__c where account__c = :ApexPages.currentPage().getParameters().get('accountId') order by createddate DESC limit 5];   	
     	return loglist;
     }
     
     public PageReference step1()
     {
     	return Page.CaseConnectionLogStep1;
     }
     
     public PageReference AccountStep1()
     {
     	return Page.AccountConnectionLogStep1;
     }
     
     public PageReference step2()
     {
     	getServerAccount();
     	connectionLog.account__c = serverAccount.Id;
     	connectionLog.case__c = connCase.Id;
     	connectionLog.Email_Address_1__c = connCase.contact.Email;
     	connectionLog.Email_Address_2__c = serverAccount.System_Access_Email__c;
     	if(connectionlog.Id != null)
     	{
     		update connectionLog;
     	}
     	else
     	{
     		insert connectionLog;	
     	}
     	
     	return Page.CaseConnectionLogStep2;
     }

     public PageReference AccountStep2()
     {
     	getServerAccount();
     	Contact emailContact;
     	emailContact = [select email from contact where id = :connectionLog.Contact__c limit 1];
     	connectionLog.account__c = serverAccount.Id;
     	connectionLog.Email_Address_1__c = emailContact.Email;
     	connectionLog.Email_Address_2__c = serverAccount.System_Access_Email__c;
     	if(connectionlog.Id != null)
     	{
     		update connectionLog;
     	}
     	else
     	{
     		insert connectionLog;	
     	}
     	
     	return Page.AccountConnectionLogStep2;
     }
     
    public PageReference returnToCase() 
    {
			PageReference casePage = new ApexPages.StandardController(connCase).view();
			casePage.setRedirect(true);
			return casePage; 
    }

    public PageReference returnToAccount() 
    {
			PageReference accountPage = new ApexPages.StandardController(serverAccount).view();
			accountPage.setRedirect(true);
			return accountPage; 
    }
     
     public PageReference saveUpdates()
     {
     	datetime dateGMT = datetime.now();
     	string dateNow = dateGMT.format();
     	if(connectionLog.VPN_Update__c != null)
     	{
	     	vpnAccount.VPN_Connection_Info__c = vpnAccount.VPN_Connection_Info__c + '\n \n' + connectionLog.VPN_Update__c + '\n' + UserInfo.getUserName() +  ' ' + dateNow;
	     	vpnAccount.VPN_Review_Needed__c = true;
	     	update vpnAccount;
     	}
     	if(connectionLog.server_Update__c != null)
     	{
	     	serverAccount.server_Connection_Info__c = ServerAccount.server_Connection_Info__c + '\n \n' + connectionLog.server_Update__c + '\n' + UserInfo.getUserName() + ' ' + dateNow;
	     	serverAccount.VPN_Review_Needed__c = true;
	     	update serverAccount;
     	}
     	if (connectionLog.Case__c != null)
     	{
     		return Page.CaseConnectionLogStep2;
     	}
     	else
     	{
     		return Page.AccountConnectionLogStep2;
     	}
     }
     
     public static testMethod void testConnectionController()
     {
     	
     	//insert case to originate test connection log from
     	Case[] ConnectionCase = new Case[]{};
        for(Integer x=0; x<1;x++)
        {
            Case c = new Case(Subject='test',Description='test',Feature_Area__c='Alarms',related_to__c='none',isVisibleInSelfService=false,current_release__c='a0O80000001i1OwEAI',accountId='0018000000MjwgQ',contactId='0038000000V0a6X');
            ConnectionCase.add(c);
        }
        insert ConnectionCase;
     	
     	//set the start page for a case connection log
     	PageReference startpage = Page.CaseConnectionLogStep1;
     	Test.setCurrentPage(startpage);
     	
     	//create url parameters for test
     	ApexPages.currentPage().getParameters().put('Id', connectionCase[0].Id);
     	ApexPages.currentPage().getParameters().put('accountId', '0018000000MjwgQ');
     	
     	//instantiate the controller for the test
     	ConnectionController controller = new ConnectionController();
     	controller.getcase();
     	controller.getServerAccount();
     	controller.getVpnAccount();
     	controller.getConnectionLog();
     	controller.getLogList();
     	controller.step1();
     	controller.step2();
     	controller.returnToCase();
     	controller.saveUpdates();
     }

}